package com.lq.learningTest.duoxiancheng;

public class DeadLock {
    private static final Object MONITOR1 = new Object();
    private static final Object MONITOR2 = new Object();

    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                synchronized (MONITOR1) {
                    System.out.println(Thread.currentThread().getName()+"持有一号锁");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (MONITOR2) {
                        System.out.println(Thread.currentThread().getName()+"持有二号锁");
                    }
                }
            }
        }, "thread1").start();

        new Thread(new Runnable() {
            public void run() {
                synchronized (MONITOR2) {
                    System.out.println(Thread.currentThread().getName()+"持有二号锁");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (MONITOR1) {
                        System.out.println(Thread.currentThread().getName()+"持有一号锁");
                    }
                }
            }
        }, "thread2").start();
    }
}
